package org.dromara.module.api;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.constant.ResponseCode;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.GsonResultBean;
import org.dromara.common.core.utils.Tools;
import org.dromara.module.domain.AdBanner;
import org.dromara.module.domain.CnPartner;
import org.dromara.module.domain.Product;
import org.dromara.module.domain.WxUser;
import org.dromara.module.service.*;
import org.dromara.system.domain.SysDictData;
import org.dromara.system.domain.SysNotice;
import org.dromara.system.domain.bo.SysDictDataBo;
import org.dromara.system.domain.vo.SysDictDataVo;
import org.dromara.system.domain.vo.SysNoticeVo;
import org.dromara.system.service.ISysConfigService;
import org.dromara.system.service.ISysDictDataService;
import org.dromara.system.service.ISysNoticeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * 首页相关
 */
@SaIgnore
@Slf4j
@RestController
@RequestMapping("/api/index")
public class IndexPageController {
    @Resource
    private ISysConfigService sysConfigService;
    @Resource
    private ISysDictDataService sysDictDataService;
    @Resource
    private ISysNoticeService sysNoticeService;
    @Resource
    private IProductService productService;
    @Resource
    private IAdBannerService adBannerService;
    @Resource
    private ParameterService parameterService;
    @Resource
    private ICnPartnerService partnerService;
    @Resource
    private IWxUserService userService;

    @Value("${ruoyi.imgUrl}")
    private String imgUrl;


    /**首页
     * @param
     * @throws Exception
     */
    @PostMapping(value="/index")
    public JSONObject indexPage(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        Map<String,Object> map = new HashMap<String, Object>();
        try{
            System.out.println("index 接受参数：" + requestParam);
            log.info("index 接受参数：" + requestParam);
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            Integer user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id"): null;
            int pageIndex = jsonRequest.containsKey("pageIndex") ? jsonRequest.getInt("pageIndex"): 0;
            int pageSize = jsonRequest.containsKey("pageSize") ? jsonRequest.getInt("pageSize"): 20;

            WxUser user = userService.getById(user_id);

            List<AdBanner> banners = adBannerService.getList(1);
            map.put("banners",banners);

            List<CnPartner> partners = partnerService.getPartnerList();
            map.put("partners", partners);

            QueryWrapper<SysNotice> qwNotice = new QueryWrapper<>();
            qwNotice.eq("notice_type", 3);
            qwNotice.eq("service_type", "industry_news");
            qwNotice.eq("status", 0);
            qwNotice.last("limit 5");
            qwNotice.orderByDesc("create_time");
            List<SysNotice> noticeList = sysNoticeService.list(qwNotice);
            for (SysNotice notice : noticeList){
                if(StrUtil.isNotEmpty(notice.getImgUrl())){
                    notice.setImgUrl(imgUrl + notice.getImgUrl());
                }
            }
            map.put("noticeList", noticeList);

            List<Product> products = productService.queryProductList(user, "", 1, 0, pageIndex, 4);
            map.put("products", products);

            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功",map),true,false);
        }catch (Exception e){
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"系统异常",e.getMessage()),true,true);
            e.getStackTrace();
            log.error(e.toString(), e);
        }
        return result;
    }


    /**
     *  服务页面 返回数据
     * @param
     * @throws Exception
     */
    @PostMapping(value="/service/type")
    public JSONObject serviceType(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        Map<String,Object> map = new HashMap<>();
        try{
            System.out.println("index 接受参数：" + requestParam);
            log.info("index 接受参数：" + requestParam);
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            String type = jsonRequest.containsKey("type") ? jsonRequest.getStr("type"): "";

            SysDictDataBo dictDataBo = new SysDictDataBo();
            dictDataBo.setDictType("service_type");
            List<SysDictDataVo> dictData = sysDictDataService.selectDictDataList(dictDataBo);
            if(StrUtil.isNotEmpty(type)){
                QueryWrapper<SysNotice> qwNotice = new QueryWrapper<>();
                qwNotice.eq("notice_type", 3);
                qwNotice.eq("service_type", type);
                qwNotice.eq("status", 0);
                qwNotice.last("limit 1");
                qwNotice.orderByDesc("create_time");
                SysNotice notice = sysNoticeService.getOne(qwNotice);
                if(notice!= null){
                    if(StrUtil.isNotEmpty(notice.getImgUrl())){
                        notice.setImgUrl(imgUrl + notice.getImgUrl());
                    }
                }
                map.put("notice", notice);
            }else{
                QueryWrapper<SysNotice> qwNotice = new QueryWrapper<>();
                qwNotice.eq("notice_type", 3);
                qwNotice.eq("service_type", dictData.get(0).getDictValue());
                qwNotice.eq("status", 0);
                qwNotice.last("limit 1");
                qwNotice.orderByDesc("create_time");
                SysNotice notice = sysNoticeService.getOne(qwNotice);
                if(notice!= null){
                    if(StrUtil.isNotEmpty(notice.getImgUrl())){
                        notice.setImgUrl(imgUrl + notice.getImgUrl());
                    }
                }
                map.put("notice", notice);
            }

            map.put("serviceType", dictData);

            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功",map),true,false);
        }catch (Exception e){
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"系统异常",e.getMessage()),true,true);
            e.getStackTrace();
            log.error(e.toString(), e);
        }
        return result;
    }


    /**
     *  咨询详情
     * @param
     * @throws Exception
     */
    @PostMapping(value="/notice/detail")
    public JSONObject noticeDetail(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        Map<String,Object> map = new HashMap<>();
        try{
            System.out.println("notice/detail 接受参数：" + requestParam);
            log.info("notice/detail 接受参数：" + requestParam);
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            Long notice_id = jsonRequest.containsKey("notice_id") ? jsonRequest.getLong("notice_id"): 0L;

            SysNoticeVo sysNotice = sysNoticeService.selectNoticeById(notice_id);
            map.put("sysNotice", sysNotice);

            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功",map),true,false);
        }catch (Exception e){
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"系统异常",e.getMessage()),true,true);
            e.getStackTrace();
            log.error(e.toString(), e);
        }
        return result;
    }


}
